package com.tcm.day03;

public class Code04_MyDeque {

    public static class MyDequeue {

        Node head;
        Node tail;
        int size;

        public MyDequeue() {
            head = null;
            tail = null;
            size = 0;
        }

        public void pushHead(int val) {
            Node node = new Node(val);
            if (head == null) {
                head = node;
                tail = head;
            } else {
                node.next = head;
                head.prev = node;
                head = node;
            }
            size++;
        }

        public void pushTail(int val) {
            Node node = new Node(val);
            if (tail == null) {
                tail = node;
                head = tail;
            } else {
                node.next = tail;
                tail.prev = node;
                tail = node;
            }
            size++;
        }

        public int pollHead() {
            int ans = -1;
            if (head != null) {
                ans = head.val;
                head = head.next;
                head.prev = null;
                size--;
            }
            if (head == null) {
                tail = null;
            }
            return ans;
        }

        public int pollTail() {
            int ans = -1;
            if (tail != null) {
                ans = tail.val;
                tail = tail.prev;
                tail.next = null;
                size--;
            }

            if (tail == null) {
                head = null;
            }
            return ans;
        }

    }

}
